package net.lahuobao.export.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import net.lahuobao.export.datasoruce.entity.Paybill;
import net.lahuobao.export.service.comm.IStreamGen;
/**
 * 导出文件工具类
 * @author Administrator
 *
 */
@Component("exportFileUtil")
@Scope("prototype")
public class ExportFileUtil {
	
	@Resource(name="txtStreamGenService")
	private IStreamGen txtStreamGenService;
	
	@Resource(name="excelStreamGenService")
	private IStreamGen excelStreamGenService;
	
	private IStreamGen currentStreamService;
	
	private String fileName;//文件名称
	private String extName;
	
	public ExportFileUtil() {
		// TODO Auto-generated constructor stub
	}
	public ExportFileUtil(String fileName) {
		this.fileName=fileName;
	}

	public String getFileName() {
		return fileName;
	}
	
	public void setFileName(String fileName , String extName) {
		this.fileName = fileName + "." + extName;
		this.extName = extName;
		
		if(extName.equals("txt")){
			currentStreamService = txtStreamGenService;
		}else if(extName.equals("xlsx")){
			currentStreamService = excelStreamGenService;
		}
	}
	
	
	
	/**
	 * 直接下载
	 * @throws UnsupportedEncodingException 
	 * @throws IOException 
	 */
	public <T>int exportIOStream(HttpServletResponse response,List<T> dataList,int limit,int begin,int index) throws UnsupportedEncodingException {
		
	    PrintWriter pw = null;
	    boolean flag = false;
	    
        try{
        	
        	if(extName.equals("txt")){
        		pw=response.getWriter();//currentStreamService = txtStreamGenService;
    		}
        	
        	
        	
        	flag=currentStreamService.createStream(pw, "", response, dataList,limit,index);
        	
        	if(flag){//还有数据
        		begin=begin+limit;
        	}
        	
        }catch(Exception e){
        	e.printStackTrace();
        }
        return begin;
	}
	
	
	// 初始化导出
	public void initExport(HttpServletResponse response){
		String fileType = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
	    try {
			response.setHeader("Content-Disposition","attachment;filename=\"" + new String(fileName.getBytes("gb2312"), "ISO8859-1") + "\"");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	    currentStreamService.init();
	}
	
	
	// 导出结束
	public void finish(HttpServletResponse response){
		currentStreamService.finish(response);
	}
	
	/**
	 *
	 *下载临时文件
	 */
	public void exportTempFile(){
		
	}
}
